package com.baidu.thirdlib;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.LayoutRes;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kvin.toolkit.activity.OnBindInit;
import com.kvin.toolkit.activity.OnHandleMessageListener;
import com.kvin.toolkit.activity.OnNavClickListener;


public class BaseActivity extends AppCompatActivity implements OnHandleMessageListener, View.OnClickListener, OnNavClickListener, OnBindInit {

    protected Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            onHandleMessage(msg);
        }
    };
    private RelativeLayout mContainer;
    private android.widget.TextView titleText;
    private Toolbar toolbar;
    private FloatingActionButton fab;

    protected boolean isInitialized = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.setContentView(R.layout.activity_base);
        this.fab = (FloatingActionButton) findViewById(R.id.fab);
        this.fab.setVisibility(View.GONE);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        this.toolbar.setTitle("");
//        this.toolbar.setNavigationIcon(R.mipmap.icon_pop_last);
        this.titleText = (TextView) findViewById(R.id.title_text);
        this.mContainer = (RelativeLayout) findViewById(R.id.base_container);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        initConfig();

        isInitialized = false;
    }


    /**
     * init config
     */
    private void initConfig() {
        this.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onFabClick(v);
            }
        });

        this.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackClick(view);
            }
        });
    }


    @Override
    public void init() {

    }

    @Override
    public void initNav() {

    }

    @Override
    public void loadingData() {

    }


    @Override
    protected void onStart() {
        super.onStart();
        if (!isInitialized) {
            isInitialized = true;
            init();
            initNav();
            //loadingData();
        }
    }


    /**
     * get context
     */
    protected Activity getActivity() {
        return this;
    }

    /**
     * add content view
     *
     * @param res
     */
    public void setContentView(@LayoutRes int res) {
        View v = LayoutInflater.from(this).inflate(res, null);
        this.mContainer.addView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


    }


    /**
     * set title
     */
    public void setTitle(String title) {
        this.titleText.setText(title);
    }


    /**
     * set fab visible
     */
    public void setFabVisibility(int visibility) {
        this.fab.setVisibility(visibility);
    }

    /**
     * set action bar visible
     */
    public void setActionBarVisible(int visibility) {
        this.toolbar.setVisibility(View.GONE);
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public void onHandleMessage(Message msg) {

    }

    @Override
    public void onBackClick(View v) {

    }

    @Override
    public void onNextClick() {

    }

    @Override
    public void onFabClick(View v) {

    }


}
